import 'package:flutter/material.dart';

/// 家庭成员角色枚举
enum FamilyRole {
  /// 家庭创建者/管理员
  admin(Icons.admin_panel_settings, '管理员'),
  /// 父亲
  father(Icons.face, '爸爸'),
  /// 母亲
  mother(Icons.face, '妈妈'),
  /// 儿子
  son(Icons.child_care, '儿子'),
  /// 女儿
  daughter(Icons.child_care, '女儿'),
  /// 祖父
  grandfather(Icons.elderly, '爷爷'),
  /// 祖母
  grandmother(Icons.elderly, '奶奶'),
  /// 外祖父
  maternalGrandfather(Icons.elderly, '外公'),
  /// 外祖母
  maternalGrandmother(Icons.elderly, '外婆'),
  /// 其他
  other(Icons.person, '其他');

  /// 角色对应的图标
  final IconData icon;
  /// 角色的中文名称
  final String displayName;

  const FamilyRole(this.icon, this.displayName);
}

/// 心情状态枚举
enum MoodStatus {
  /// 开心
  happy(Icons.sentiment_very_satisfied, '开心', Colors.green),
  /// 平静
  calm(Icons.sentiment_satisfied, '平静', Colors.blue),
  /// 疲惫
  tired(Icons.sentiment_neutral, '疲惫', Colors.orange),
  /// 生气
  angry(Icons.sentiment_very_dissatisfied, '生气', Colors.red),
  /// 悲伤
  sad(Icons.sentiment_dissatisfied, '悲伤', Colors.purple),
  /// 忙碌
  busy(Icons.hourglass_full, '忙碌', Colors.amber);

  /// 心情对应的图标
  final IconData icon;
  /// 心情的中文名称
  final String displayName;
  /// 心情对应的颜色
  final Color color;

  const MoodStatus(this.icon, this.displayName, this.color);
}

/// 家庭成员模型
class FamilyMember {
  /// 成员唯一标识
  final String id;
  /// 成员名称
  final String name;
  /// 成员角色
  final FamilyRole role;
  /// 成员生日
  final DateTime? birthday;
  /// 成员头像URL
  final String? avatarUrl;
  /// 当前心情
  final MoodStatus? mood;
  /// 心情更新时间
  final DateTime? moodUpdatedAt;
  /// 是否是当前用户
  final bool isCurrentUser;
  /// 成员手机号
  final String? phoneNumber;
  /// 成员邮箱
  final String? email;

  /// 构造函数
  FamilyMember({
    required this.id,
    required this.name,
    required this.role,
    this.birthday,
    this.avatarUrl,
    this.mood,
    this.moodUpdatedAt,
    this.isCurrentUser = false,
    this.phoneNumber,
    this.email,
  });

  /// 从JSON创建成员对象
  factory FamilyMember.fromJson(Map<String, dynamic> json) {
    return FamilyMember(
      id: json['id'] as String,
      name: json['name'] as String,
      role: FamilyRole.values.firstWhere(
        (role) => role.name == json['role'],
        orElse: () => FamilyRole.other,
      ),
      birthday: json['birthday'] != null
          ? DateTime.parse(json['birthday'] as String)
          : null,
      avatarUrl: json['avatarUrl'] as String?,
      mood: json['mood'] != null
          ? MoodStatus.values.firstWhere(
              (mood) => mood.name == json['mood'],
              orElse: () => MoodStatus.calm,
            )
          : null,
      moodUpdatedAt: json['moodUpdatedAt'] != null
          ? DateTime.parse(json['moodUpdatedAt'] as String)
          : null,
      isCurrentUser: json['isCurrentUser'] as bool? ?? false,
      phoneNumber: json['phoneNumber'] as String?,
      email: json['email'] as String?,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'role': role.name,
      'birthday': birthday?.toIso8601String(),
      'avatarUrl': avatarUrl,
      'mood': mood?.name,
      'moodUpdatedAt': moodUpdatedAt?.toIso8601String(),
      'isCurrentUser': isCurrentUser,
      'phoneNumber': phoneNumber,
      'email': email,
    };
  }

  /// 创建成员副本并更新指定字段
  FamilyMember copyWith({
    String? id,
    String? name,
    FamilyRole? role,
    DateTime? birthday,
    String? avatarUrl,
    MoodStatus? mood,
    DateTime? moodUpdatedAt,
    bool? isCurrentUser,
    String? phoneNumber,
    String? email,
  }) {
    return FamilyMember(
      id: id ?? this.id,
      name: name ?? this.name,
      role: role ?? this.role,
      birthday: birthday ?? this.birthday,
      avatarUrl: avatarUrl ?? this.avatarUrl,
      mood: mood ?? this.mood,
      moodUpdatedAt: moodUpdatedAt ?? this.moodUpdatedAt,
      isCurrentUser: isCurrentUser ?? this.isCurrentUser,
      phoneNumber: phoneNumber ?? this.phoneNumber,
      email: email ?? this.email,
    );
  }

  /// 获取年龄
  int? get age {
    if (birthday == null) return null;
    final now = DateTime.now();
    int age = now.year - birthday!.year;
    if (now.month < birthday!.month ||
        (now.month == birthday!.month && now.day < birthday!.day)) {
      age--;
    }
    return age;
  }

  /// 获取生日格式化字符串
  String get birthdayFormatted {
    if (birthday == null) return '未设置';
    return '${birthday!.year}年${birthday!.month}月${birthday!.day}日';
  }

  /// 获取心情更新时间格式化字符串
  String? get moodUpdatedAtFormatted {
    if (moodUpdatedAt == null) return null;
    final now = DateTime.now();
    final difference = now.difference(moodUpdatedAt!);
    
    if (difference.inMinutes < 1) {
      return '刚刚';
    } else if (difference.inHours < 1) {
      return '${difference.inMinutes}分钟前';
    } else if (difference.inDays < 1) {
      return '${difference.inHours}小时前';
    } else if (difference.inDays < 30) {
      return '${difference.inDays}天前';
    } else {
      return '${moodUpdatedAt!.year}年${moodUpdatedAt!.month}月${moodUpdatedAt!.day}日';
    }
  }
}
